package com.wanmait.movie.controller;

import com.wanmait.movie.entity.Admin;
import com.wanmait.movie.entity.Cinema;
import com.wanmait.movie.entity.request.manage.cinema.CinemaUpdateRequest;
import com.wanmait.movie.service.CinemaService;
import com.wanmait.movie.util.AjaxResult;
import com.wanmait.movie.util.JWTUtils;
import com.wanmait.movie.util.UploadFileUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;
import java.io.IOException;
import java.util.Date;

/**
 * ClassName: ManageMovieController
 * Package: com.wanmait.movie.controller
 * Description:
 * 影院 controller
 * @Author Fengxd
 * @Create 2023/12/15 9:58
 * @Version 1.0
 */
@RestController
@RequestMapping("manage/cinema")
public class ManageCinemaController {
    @Resource
    private CinemaService cinemaService;

    @Resource
    private UploadFileUtil uploadFileUtil;
    /**
     * 获取影院列表-分页
     * @return 影院列表
     */
    @GetMapping("getCinemaInfo")
    public AjaxResult getMovieInfo(@Min(1) @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return AjaxResult.success(cinemaService.getCinemaInfo(pageNum, pageSize));
    }

    /**
     * 获取影院列表
     */
    @GetMapping("allCinemaInfo")
    public AjaxResult allCinemaInfo() {
        return AjaxResult.success(cinemaService.getCinemaInfo());
    }
    /**
     * 根据 ID 获取影院详细信息
     * @param id id
     * @return 影院详细信息
     */
    @GetMapping("getDetailed")
    public AjaxResult getCinemaInfoById(@RequestParam() Integer id) {
        return AjaxResult.success(cinemaService.getCinemaInfoById(id));
    }

    @PostMapping("updateCinema")
    public AjaxResult updateCinema(@RequestBody CinemaUpdateRequest cinemaUpdateRequest,
                                   HttpServletRequest request) {
        Cinema cinema = new Cinema();
        BeanUtils.copyProperties(cinemaUpdateRequest,cinema);
        // 更新时间
        cinema.setUpdateTime(new Date());
        // 更新人
        Admin admin = (Admin) request.getAttribute("admin");
        cinema.setUpdateAdminId(admin.getId());
        return AjaxResult.success(cinemaService.updateById(cinema));
    }

    /**
     * 添加影院
     */
    @PostMapping("addCinema")
    public AjaxResult addCinema(@RequestBody Cinema cinema, HttpServletRequest request) {
        // 新增时间
        cinema.setInsertTime(new Date());
        // 新增人
        Integer currentAdminId = Integer.parseInt(JWTUtils.getAudience(request.getHeader("token")));
        cinema.setInsertAdminId(currentAdminId);
        return AjaxResult.success(cinemaService.save(cinema));
    }

    @GetMapping("deleteCinema")
    public AjaxResult deleteCinema(@RequestParam() Integer id) {
        return AjaxResult.success(cinemaService.removeById(id));
    }

    /**
     * 上传影院图片
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("uploadPicture")
    public AjaxResult uploadPicture(@RequestParam("file")MultipartFile file) throws IOException {
        String fileUrl = uploadFileUtil.upload(file, "");
        if (fileUrl == null || fileUrl.isEmpty()) {
            return AjaxResult.error("上传失败");
        }
        return AjaxResult.success(fileUrl);
    }
    @GetMapping("cinemaName")
    public AjaxResult cinemaName(){
        return AjaxResult.success(cinemaService.list());
    }
}
